package com.qczl.vehicle.controller;

import com.qczl.common.core.utils.poi.ExcelUtil;
import com.qczl.common.core.web.controller.BaseController;
import com.qczl.common.core.web.domain.AjaxResult;
import com.qczl.common.core.web.page.TableDataInfo;
import com.qczl.common.log.annotation.Log;
import com.qczl.common.log.enums.BusinessType;
import com.qczl.common.security.annotation.RequiresPermissions;
import com.qczl.vehicle.domain.Vehicles;
import com.qczl.vehicle.domain.vo.VehiclesVo;
import com.qczl.vehicle.service.IVehiclesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @Author: 钟城明
 * @CreateTime: 2025-09-20
 * @Description:
 * @Version: 1.0
 */
@Tag(name = "车辆管理")
@RestController
@RequestMapping("/vehicles")
public class VehiclesController extends BaseController {
    @Autowired
    private IVehiclesService vehiclesService;

    /**
     * 分页查询车辆列表
     */
    @Operation(summary = "查询车辆列表")
    @GetMapping("list")
    public TableDataInfo list(VehiclesVo vehiclesVo) {
        startPage();
        List<VehiclesVo> list = vehiclesService.selectVehicleList(vehiclesVo);
        return getDataTable(list);
    }

    @Operation(summary = "导出车辆列表")
    @RequiresPermissions("vehicle:vehicles:export")
    @Log(title = "车辆管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VehiclesVo vehiclesVo) {
        List<VehiclesVo> list = vehiclesService.selectVehicleList(vehiclesVo);
        ExcelUtil<VehiclesVo> util = new ExcelUtil<>(VehiclesVo.class);
        util.exportExcel(response, list, "车辆列表数据");
    }

    /**
     * 获取车辆详细信息
     */
    @Operation(summary = "获取车辆详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(vehiclesService.getById(id));
    }

    /**
     * 新增车辆
     */
    @Operation(summary = "新增车辆")
    @PostMapping
    public AjaxResult add(@RequestBody VehiclesVo vehiclesVo) {
        Vehicles vehicles = new Vehicles();
        BeanUtils.copyProperties(vehiclesVo, vehicles);
        return toAjax(vehiclesService.saveVehicleWithBrandName(vehicles, vehiclesVo.getBrandName()));
    }

    /**
     * 修改车辆
     */
    @Operation(summary = "修改车辆")
    @PutMapping("{id}")
    public AjaxResult edit(@PathVariable Long id, @RequestBody VehiclesVo vehiclesVo) {
        return toAjax(vehiclesService.updateVehicleWithBrandName(id, vehiclesVo));
    }

    /**
     * 删除车辆
     */
    @Operation(summary = "删除车辆")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(vehiclesService.removeByIds(Arrays.asList(ids)));
    }
}
